var app = getApp();
var openid = (wx.getStorageSync('openid'));
Page({
  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    // 左右滑动
    currentTab: 0,
    fcash: '',
    scash: '',
    user_name: '',
    user_img: '',
    onplay:true,  //录音播放状态
    cash: '0',
    scroll_height: 0,
  },
  /*** 点击tab切换***/
  swichNav: function (e) {
    var that = this;
    that.setData({
      currentTab: e.target.dataset.current,
      scroll_height: 0,
      page: 1,
    });
    that.onLoad();
  },
  /*** 滑动切换tab***/
  bindChange: function (e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current,
      scroll_height: 0,
      page: 1,
    });
    that.onLoad();
  },

  // 查看详情（我发出的 and 我收到的）
  detail_click: function (e) {
    wx.navigateTo({
      url: '/pages/voice_box/guess/index?id=' + e.currentTarget.dataset.id,
    })
  },


  //下拉加载
  drop_load: function (e) {
    wx.showLoading({ title: 'loading', mask: true })  
    var that = this;
    app.token_md5();      
    var url;
    if (that.data.currentTab == 0) {
      url = "api/cdmyRedf";
    } else {
      url = "api/cdmyReds";
    }
    wx.request({
      url: app.url2 + url,
      data: {
        openid: wx.getStorageSync('openid'),
        page: ++that.data.page,
        token: wx.getStorageSync('token_md5'),
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      method: 'GET',
      success: function (res) {
        console.log(res.data);
        if (that.data.currentTab == 0) {
          console.log('我发出的');
          if (res.data.redfList.data == '') {
            wx.showToast({
              title: '没有更多了哦',
              icon: 'loading',
              duration: 1200
            })
          } else {
            var record = that.data.record;
            for (var i in res.data.redfList.data) {
              record.push(res.data.redfList.data[i])
            }
            that.setData({
              page: that.data.page,
              record: record,
            })
          }
        } else {
          console.log('我收到的');
          if (res.data.redsList.data == '') {
            wx.showToast({
              title: '没有更多了哦',
              icon: 'loading',
              duration: 1200
            })
          } else {
            var receive = that.data.receive;

            for (var i in res.data.redsList.data) {
              receive.push(res.data.redsList.data[i])
            }
            that.setData({
              page: that.data.page,
              receive: receive,
            })
          }

        }
        wx.hideLoading();                
      },
    })
  },

onShow:function(){
  wx.showLoading({ title: 'loading', mask: true })
  var that = this;
  app.token_md5();      
  var url;
  if (that.data.currentTab == 0) {
    url = "api/cdmyRedf";
  } else {
    url = "api/cdmyReds";
  }
  that.setData({
    user_img: wx.getStorageSync('avatarurl'),
    user_name: wx.getStorageSync('nickname'),
  })
  wx.request({
    url: app.url2 + url,
    header: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: {
      openid: wx.getStorageSync('openid'),
      token: wx.getStorageSync('token_md5'),
    },
    method: 'GET',
    success: function (res) {
      var data = res.data;
      // console.log(data);
      // 时间转化  以及  金额计算
      // var fcash = 0, scash = 0;
      // fcash = camd(fcash, data.redsList.data);
      // scash = camd(scash, data.redsList.data);
      // function camd(name, list) {
      //   for (var i in list) {
      //     name += parseInt(scash) + list[i].price;
      //     list[i].created_at = (getLocalTime(list[i].created_at));
      //     list[i].price = parseInt(list[i].price).toFixed(2);
      //   }
      //   return name;
      // }
      if (that.data.currentTab == 0) {
        that.setData({
          record: data.redfList.data,
          f_total: data.redfList.total,
          f_fcash: data.sum,
        })
      } else {
        console.log(data);
        that.setData({
          receive: data.redsList.data,
          s_total: data.redsList.total,
          s_fcash: data.sum,
        })
      }
      wx.hideLoading();
    },
    fail: function (res) {
      console.log(res.data)
      wx.hideLoading();
    }
  });
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({ title: 'loading', mask: true })    
    var that = this;
    app.token_md5();      
    var url;
    if (that.data.currentTab == 0) {
      url = "api/cdmyRedf";
    } else {
      url = "api/cdmyReds";
    }
    that.setData({
      user_img: wx.getStorageSync('avatarurl'),
      user_name: wx.getStorageSync('nickname'),
    })
    wx.request({
      url: app.url2 + url,
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        openid: wx.getStorageSync('openid'),
        token: wx.getStorageSync('token_md5'),
      },
      method: 'GET',
      success: function (res) {
        var data = res.data;
        // console.log(data);
        // 时间转化  以及  金额计算
        // var fcash = 0, scash = 0;
        // fcash = camd(fcash, data.redsList.data);
        // scash = camd(scash, data.redsList.data);
        // function camd(name, list) {
        //   for (var i in list) {
        //     name += parseInt(scash) + list[i].price;
        //     list[i].created_at = (getLocalTime(list[i].created_at));
        //     list[i].price = parseInt(list[i].price).toFixed(2);
        //   }
        //   return name;
        // }
        if (that.data.currentTab == 0) {
          that.setData({
            record: data.redfList.data,
            f_total: data.redfList.total,
            f_fcash: data.sum,
          })
        } else {
          console.log(data);
          that.setData({
            receive: data.redsList.data,
            s_total: data.redsList.total,
            s_fcash: data.sum,
          })
        }
        wx.hideLoading();        
      },
      fail: function (res) {
        console.log(res.data)
        wx.hideLoading();                
      }
    });
  },

onReady: function () {
    this.innerAudioContext = wx.createInnerAudioContext();
  },
})



function getLocalTime(nS) {

  var Y, M, D, W, H, I, S;
  function fillZero(v) {
    if (v < 10) { v = '0' + v; }
    return v;
  }
  var d = new Date(parseInt(nS) * 1000);
  M = fillZero(d.getMonth() + 1);
  D = fillZero(d.getDate());
  H = fillZero(d.getHours());
  I = fillZero(d.getMinutes());
  var localTime = M + '月' + D + '日 ' + H + ':' + I;
  return localTime;
}